package aad.storyboard.model;

import aad.storyboard.controller.GUIComposer;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.Log;

public class RotateAnimation extends Animation{

	private int degrees;
	private float intervalMovement;
	private float totalRotated;

	/**
	 *  rotate an object of intervalMovement at each step, this is calculated by the duration and the extent of the rotation
	 */
	public RotateAnimation(int deg, int duration,int time) {
		super(duration,time);
		this.degrees = deg;
		intervalMovement = (float) this.degrees/intervals;
		this.totalRotated = 0;
	}
	
	

	public RotateAnimation(int deg, int duration) {
		super(duration);
		this.degrees = deg;
		intervalMovement = (float) this.degrees/intervals;
	}
	

	/**
	 *  moves an object of one intervalMovement using transformation, then sets the bitmap again
	 */
	@Override
	public void animate() {
			for(Image obj : this.images){
				Bitmap mBitmap = obj.getBitmap();
				// compute the absolute rotation
		        totalRotated = (intervalMovement) % 360;
		        // precompute some trig functions
		        double radians = Math.toRadians(totalRotated);
		        double sin = Math.abs(Math.sin(radians));
		        double cos = Math.abs(Math.cos(radians));
		        // figure out total width and height of new bitmap
		        double newWidth = mBitmap.getWidth() * cos + mBitmap.getHeight() * sin;
		        double newHeight = mBitmap.getWidth() * sin + mBitmap.getHeight() * cos;

		        
		        // calculate the scale - in this case = 0.4f
		        float scaleWidth = ((float) newWidth) / obj.getWidth();
		        float scaleHeight = ((float) newHeight) / obj.getHeight();
		        
		        // createa matrix for the manipulation
		        Matrix matrix = new Matrix();
		        // resize the bit map
		       matrix.postScale(scaleWidth, scaleHeight);
		        // rotate the Bitmap
		        System.out.println("Roatating"+intervalMovement);
		        matrix.postRotate(intervalMovement,obj.getPreciseWidth(),obj.getPreciseHeight());
		      //  matrix.postRotate(intervalMovement);

		        // recreate the new Bitmap
		        Bitmap rotated = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, false);
		    
		        // make a Drawable from Bitmap to allow to set the BitMap 
		        // to the ImageView, ImageButton or what ever
		        obj.setBitmapBuffer(rotated);
		        
		        obj.setWidth((float)newWidth);
		        obj.setHeight((float)newHeight);
			}
		
	}

	/**
	 *  rerotates the object in the oposite direction such that it restores its initial state before the animation
	 */
	@Override
	public void undo() {

		for(Image obj : this.images){
			Matrix transMatrix = new Matrix();
			transMatrix.postRotate(-degrees);
			Bitmap origBMap = obj.getBitmapBuffer();
			Bitmap rotatedBitmap = Bitmap.createBitmap(origBMap, 0, 0,obj.getWidth(), obj.getHeight(), transMatrix, true); 
			obj.setBitmapBuffer(rotatedBitmap);
		}
		
	}



	@Override
	public void adjustParameters() {
		this.degrees = Math.round(this.currentDuration * this.intervalMovement);
	}

}
